package com.kk.gray.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

@Configuration
public class FeignConfiguration implements RequestInterceptor, ServletRequestListener {

    private HttpServletRequest request;

    @Override
    public void requestInitialized(ServletRequestEvent event) {
        request = (HttpServletRequest) event.getServletRequest();
    }


    // 配置所有的feig 都加请求头
    @Override
    public void apply(RequestTemplate requestTemplate) {
        RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(), true);
        HttpServletRequest request1 = this.request;
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes ( );
        HttpServletRequest request = attributes.getRequest ( );
        requestTemplate.header ("userId", request.getHeader ("userId"));
    }

}
